package com.yx.learn.service.impl;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yx.learn.dao.UserDao;
import com.yx.learn.domain.User;
import com.yx.learn.service.UserService;


@Service("userService")
@Transactional
public class UserServiceImpl implements UserService{
	
	// 在指定Autowired注解时，同时指定Qualifier注解指定bean的名字
	@Autowired
	@Qualifier("userDao")
	private UserDao userDao;

	//通过用户账号密码检查用户是否存在
	public User checkUser(String employNo, String password) throws Exception {
		// TODO Auto-generated method stub
		return this.userDao.findByNoPwd(employNo, password);
	}

	public void saveUser(User user) throws Exception {
		// TODO Auto-generated method stub
		this.userDao.saveUser(user);
		
	}

	public void deleteUser(User user) throws Exception {
		// TODO Auto-generated method stub
		this.userDao.deleteUser(user);
	}

	public void updateUser(User user) throws Exception {
		// TODO Auto-generated method stub
		this.userDao.updateUser(user);
	}

	public void login_ud(User user) throws Exception {
		// TODO Auto-generated method stub
		userDao.updateUser(user);
	}

	public User findById(int id) throws Exception {
		// TODO Auto-generated method stub
		return this.userDao.findById(id);
	}

	public User findUserByNo(String employNo) throws Exception {
		// TODO Auto-generated method stub
		return this.userDao.findUserByNo(employNo);
	}

	public int findRows(String hql) throws Exception {
		// TODO Auto-generated method stub
		return this.userDao.findRows(hql);
	}

	public List<User> findUsers(String hql, int pageNumber, int pageSize)
			throws Exception {
		// TODO Auto-generated method stub
		return this.userDao.findUsers(hql, pageNumber, pageSize);
	}

	public List<User> findAllUser(String hql) throws Exception {
		// TODO Auto-generated method stub
		return this.userDao.findAllUser(hql);
	}

	public List<User> getUsers() throws Exception {
		// TODO Auto-generated method stub
		return this.userDao.getUsers();
	}

	public String usersToCombobox() throws Exception {
		// TODO Auto-generated method stub
		List<User> users = this.userDao.getUsers();
		JSONArray ja = new JSONArray();
		for(int i=0;i<users.size();i++){
			JSONObject jo = new JSONObject();
			jo.put("uid", users.get(i).getId());
			jo.put("employName", users.get(i).getEmployName());
			ja.add(jo);
		}
		String json = ja.toString();
		return json;

	}

}
